<?php
//-----------------------------
//---include 
require( '../wp-load.php' );
//---get user_id
global $current_user;
get_currentuserinfo();
$user_id = $current_user->ID;
//-----------------------------

include_once ("connect.php");
include_once ("log_order.php"); //test!

$action = $_GET['action'];
switch ($action) {

	case 'list' : //列表		
		$page =  $_GET['page'];
		$limit = $_GET['rows'];
		$sidx =  $_GET['sidx'];
		$sord =  $_GET['sord'];
//		$page = 1;
//		$limit = 12;
//		$sidx = 'id';
//		$sord = 'asc';
		if (!$sidx){
			$sidx = 1;
		}

        $where = '';		
		//$user_id  = $_GET['uid'];		
        if( empty($user_id)){
            $where .= " and 1>1";  //禁止查出结果
		}else if($user_id > 1){
			$where .= " and order_user_id=".$user_id ;
		}
		
		$order_id  = $_GET['order_id'];		
        if(!empty($order_id)){
            $where .= " and id=".$order_id ;
		}
		
		$package_seq = $_GET['package_seq'];		
        if(!empty($package_seq)){
			$package_seq2 = unescape($package_seq);
            $where .= " and package_seq like '%".$package_seq2."%'";
		}
		
        if(!empty($_GET['order_status'])){
            $where .= " and order_status=".$_GET['order_status'];
		}
		
		$sql = "SELECT COUNT(*) AS count FROM order_list WHERE valid=1".$where;
		//$logger->debug($sql);  //test!						
		$result = mysql_query($sql);
		$row = mysql_fetch_array($result, MYSQL_ASSOC);
		$count = $row['count'];		
		if ($count > 0) {
			$total_pages = ceil($count / $limit);
		} else {
			$total_pages = 0;
		}
		if ($page > $total_pages)
			$page = $total_pages;
		$start = $limit * $page - $limit;
		if ($start<0) $start = 0;
		$sql = "SELECT * FROM order_list WHERE valid=1".$where." ORDER BY $sidx $sord LIMIT $start , $limit";
		//$logger->debug($sql);  //test!
		$result = mysql_query($sql) or die("Couldn t execute query." . mysql_error());

		$responce->page = $page;
		$responce->total = $total_pages;
		$responce->records = $count;
		$i = 0;  //['订单号','物品名称','收件人','物流商','包裹号','金额','建单时间','状态','操作']
		while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
			$responce->rows[$i]['id'] = $row[id];
			$opt = "";
			if( $user_id != 1 ){
				$opt .= "<a href='order_detail.php?id=".$row['id']."'>详细</a>";
			}			
			if($user_id==1){
				$opt .= "<a href='order_handle.php?id=".$row['id']."'>处理</a>";
			}			
			if($row['order_status']<30 || $user_id==1){   //状态_10新订单_20待付款_30已付款_40待补款_45已补款_50已结束	
				$opt .= "&nbsp;&nbsp;<a href='order_address.php?id=".$row['id']."'>地址</a>";
				$opt .= "&nbsp;&nbsp;<a href='order_item.php?id=".$row['id']."'>物品</a>";
			}	
			
			//---
			$sql1 = "SELECT idc_name FROM order_address WHERE id=".$row['address_to_id'];
			$result1 = mysql_query($sql1);	
			if ($row1 = mysql_fetch_array($result1, MYSQL_ASSOC)) {
				$address2 = $row1['idc_name'];
			}
			
			//---
			$fee_sum = '';
			$fee_add = '';
			if($row['fee_currency']==1){
				$fee_sum = '￥'.$row['fee_sum'];
				$fee_add = '￥'.$row['fee_add'];
			}else if($row['fee_currency']==2){
				$fee_sum = '€'.$row['fee_sum'];
				$fee_add = '€'.$row['fee_add'];
			}			
			
			//---
			$order_status = '待定';  		
			switch ($row['order_status']) {
				case 10 : 
					$order_status = '新建订单，待录入信息';
					break;					
				case 20 : 
					$order_status = '信息填写完成，待付款';
					break;
				case 30 : 
				case 45 : 
					$order_status = '支付完成，转运中';
					break;					
				case 40 : 
					$order_status = '款项不足，待补款'.$fee_add;
					break;
				case 50 : 
					$order_status = '订单已结束';
					break;					
			}
			
			 
			//---
			$create_time = substr($row['time_create'],5,11);
			
			//------
			$responce->rows[$i]['cell'] = array (
				$row['id'],
				$row['shipper'],
				$row['package_seq'],
				$address2,	
				$fee_sum,
				$row['package_name'],
				$create_time,
				$order_status,	
				$opt
			);
			$i++;
		}
		echo json_encode($responce);		
		break;

	case 'del' : //删除
		$ids = $_POST['ids'];
		
		//$logger->debug("ids=".$ids);  //test!		
		//$logger->debug("link=".$db_link);  //test!		
		$sql = "UPDATE order_list set valid=0,update_time=now() where id IN($ids)";
		$logger->debug($sql);  //test!
		mysql_query($sql);	
		if (mysql_affected_rows($db_link)) {
			echo $ids;
		} else {
			die("0");
		} 
		
		//delAllSelect($ids, $db_link);	 ???	
		break;


	case 'han' : //处理订单
		$order_id = htmlspecialchars(stripslashes(trim($_POST['order_id'])));		
		$fee_pay = htmlspecialchars(stripslashes(trim($_POST['fee_pay'])));		
		$fee_currency = htmlspecialchars(stripslashes(trim($_POST['fee_currency'])));
		$fee_mode = htmlspecialchars(stripslashes(trim($_POST['fee_mode'])));
		$fee_add = htmlspecialchars(stripslashes(trim($_POST['fee_add'])));		
		$package_seq = htmlspecialchars(stripslashes(trim($_POST['package_seq'])));		
		$order_status = htmlspecialchars(stripslashes(trim($_POST['order_status'])));		
		
		//$logger->debug("fee_pay=".$fee_pay." fee_add=".$fee_add);  //test!
				
		if(empty($fee_pay) || $fee_pay == ''){
            $fee_pay=0 ;
		}		
		if(empty($fee_add) || $fee_add == ''){
            $fee_add=0;
		}
		
		$set_sql="";
		if($order_status == 30){ //已付款
			$set_sql .= ",time_fee=now()";
		}else if($order_status == 50){ //已结束
			$set_sql .= ",time_finish=now()";
		}
		
		$sql = "UPDATE order_list SET order_status=$order_status,package_seq='$package_seq',fee_mode=$fee_mode,fee_currency=$fee_currency,fee_pay=$fee_pay,fee_add=$fee_add".$set_sql.",update_time=now() WHERE id=$order_id";	
		$logger->debug($sql);  //test!
		$rs = mysql_query($sql);			
		
		$logger->debug("order_id=".$order_id);  //test!
		
		echo $order_id;
		
		break;	
		
	case 'han_fee' : //修改总金额
		$order_id = htmlspecialchars(stripslashes(trim($_POST['order_id'])));		
		$fee_sum = htmlspecialchars(stripslashes(trim($_POST['fee_sum'])));		
		$fee_currency = htmlspecialchars(stripslashes(trim($_POST['fee_currency'])));				

		$sql = "UPDATE order_list SET fee_currency=$fee_currency,fee_sum=$fee_sum,update_time=now() WHERE id=$order_id";	
		$logger->debug($sql);  //test!
		$rs = mysql_query($sql);			
		
		$logger->debug("order_id=".$order_id);  //test!
		
		echo $order_id;
		
		break;			

	case '' :
		echo 'Bad request.';
		break;
}

//批量删除操作
function delAllSelect($ids, $link222) {

	$logger->debug("ids===".$ids);  //test!		
	$logger->debug("link===".$link222);  //test!		
	$sql = "UPDATE order_list set valid=0,update_time=now() where id IN($ids)";

	$logger->debug($sql);  //test!
	mysql_query($sql);	
	if (mysql_affected_rows($link222)) {
		return $ids;
	} else {
		die("0");
	} 
}


//------------------
//处理接收jqGrid提交查询的中文字符串
function unescape($str) 
{ 
    $ret = ''; 
    $len = strlen($str); 
    for ($i = 0; $i < $len; $i ++) 
    { 
        if ($str[$i] == '%' && $str[$i + 1] == 'u') 
        { 
            $val = hexdec(substr($str, $i + 2, 4)); 
            if ($val < 0x7f) 
                $ret .= chr($val); 
            else  
                if ($val < 0x800) 
                    $ret .= chr(0xc0 | ($val >> 6)) . 
                     chr(0x80 | ($val & 0x3f)); 
                else 
                    $ret .= chr(0xe0 | ($val >> 12)) . 
                     chr(0x80 | (($val >> 6) & 0x3f)) . 
                     chr(0x80 | ($val & 0x3f)); 
            $i += 5; 
        } else  
            if ($str[$i] == '%') 
            { 
                $ret .= urldecode(substr($str, $i, 3)); 
                $i += 2; 
            } else 
                $ret .= $str[$i]; 
    } 
    return $ret; 
}



?>